<?php

namespace App\Http\Controllers\Admin\SuperAdmin;

use App\Exceptions\SuperAdminException;
use App\Services\SuperAdmin\AuthorityService;
use Exception;
use Illuminate\Http\JsonResponse;

class AuthorityController extends BaseSuperAdminController
{

    protected AuthorityService $authorityService;

    /**
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->authorityService = loadService('SuperAdmin\AuthorityService');
    }

    /**
     * 创建角色
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function createAuthority(): JsonResponse
    {
        $rule = [
            'authorityId'   => 'required|integer',
            'authorityName' => 'required|string|min:1',
            'parentId'      => 'required|integer',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $result = $this->authorityService->createAuthority($this->params);
        return outputJsonResult($result);
    }

    /**
     * 更新角色
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function updateAuthority(): JsonResponse
    {
        $rule = [
            'authorityId'   => 'required|integer',
            'authorityName' => 'required|string|min:1',
            'parentId'      => 'required|integer',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->authorityService->updateAuthority($this->params);
        return outputJsonResult();
    }

    /**
     * 删除角色
     *
     * @return JsonResponse
     * @throws SuperAdminException
     */
    public function deleteAuthority(): JsonResponse
    {
        $rule = [
            'authorityId' => 'required|integer',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->authorityService->deleteAuthority($this->params);
        return outputJsonResult();
    }

    /**
     * 角色列表
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function authorityList(): JsonResponse
    {

        $list = $this->authorityService->authorityList();

        return outputJsonResult($list);
    }
}
